package edu.hziee.controllers;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import edu.hziee.dao.UserDao;
import edu.hziee.models.User;

@Controller
@RequestMapping("/register")
public class RegisterController {
	@Autowired
	private UserDao userDao;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView get() {
		return new ModelAndView("login/register");
	}

	@RequestMapping(method = RequestMethod.POST)
	public ModelAndView post(@RequestParam String userName, @RequestParam String password) {

		User user = new User();
		
		if (userDao.findByName(userName) == null) {
			user.setUserName(userName);
			user.setPassword(password);
			userDao.addUser(user);
			
			ModelAndView mav = new ModelAndView("login/login");
			mav.addObject("error", "恭喜您已成功注册!");
			return mav;
		}else{
			ModelAndView mav = new ModelAndView("login/register");
			mav.addObject("error", "该用户已存在!");
			return mav;
		}

		

	}
}
